package com.moon.interview.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moon.interview.exception.SysErrorCode;
import com.moon.interview.mapper.QuestionBankQuestionMapper;
import com.moon.interview.model.dto.questionBankQuestion.QuestionBankQuestionBatchRequest;
import com.moon.interview.model.dto.questionBankQuestion.QuestionBankQuestionRequest;
import com.moon.interview.model.entity.QuestionBank;
import com.moon.interview.model.entity.QuestionBankQuestion;
import com.moon.interview.model.entity.User;
import com.moon.interview.model.vo.QuestionBankQuestionVo;
import com.moon.interview.service.IQuestionBankQuestionService;
import com.moon.interview.service.IQuestionBankService;
import com.moon.interview.utils.ThrowUtils;
import com.moon.interview.utils.UserUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 题目(QuestionBankQuestion)表服务实现类
 *
 * @author MoonX
 * @since 2025-03-14 14:12:40
 */
@Service
@Slf4j
public class QuestionBankQuestionServiceImpl extends ServiceImpl<QuestionBankQuestionMapper, QuestionBankQuestion> implements IQuestionBankQuestionService {

    @Resource
    private IQuestionBankService questionBankService;

    @Override
    public Boolean addQuestionBankQuestion(QuestionBankQuestionRequest addRequest) {
        // 获取当前登录信息
        User user = UserUtils.getLoginUser();

        Long questionId = addRequest.getQuestionId();
        List<Long> questionBankIdList = addRequest.getQuestionBankIdList();
        // 题目信息不能为空
        ThrowUtils.throwIf(questionId == null,SysErrorCode.PARAMS_ERROR,"题目Id不能为空");
        // 题库信息不能为空
        ThrowUtils.throwIf(questionBankIdList == null || questionBankIdList.isEmpty(),SysErrorCode.PARAMS_ERROR,"题库Id列表不能为空");
        // 添加题目题库关联信息
        questionBankIdList.stream().forEach(li->{
            QuestionBankQuestion questionBankQuestion = new QuestionBankQuestion();
            questionBankQuestion.setUserId(user.getId());
            questionBankQuestion.setQuestionBankId(li);
            questionBankQuestion.setQuestionId(questionId);
            boolean flag = this.save(questionBankQuestion);
            // 后期可以记录是哪个题库添加失败
            if (!flag) {
                log.error("题目Id: {}, 题库Id：{}，添加题目题库关联信息失败",questionId,li);
            }
        });
        return true;
    }

    @Override
    public Boolean updateQuestionBankQuestion(QuestionBankQuestionRequest updateRequest) {
        List<Long> questionBankIdList = updateRequest.getQuestionBankIdList();
        Long questionId = updateRequest.getQuestionId();
        // 删除题目原先的题目关联信息
        this.remove(Wrappers.<QuestionBankQuestion>lambdaUpdate().eq(QuestionBankQuestion::getQuestionId, updateRequest.getQuestionId()));
        if (questionBankIdList == null || questionBankIdList.isEmpty()) {
            return true;
        }
        // 添加题目题库关联信息
        questionBankIdList.stream().forEach(li->{
            QuestionBankQuestion questionBankQuestion = new QuestionBankQuestion();
            questionBankQuestion.setQuestionBankId(li);
            questionBankQuestion.setQuestionId(questionId);
            this.save(questionBankQuestion);
        });
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)  // 开启事务
    public Boolean addBatchQuestionBankQuestion(QuestionBankQuestionBatchRequest batchRequest) {
        List<Long> questionIdList = batchRequest.getQuestionIdList();
        List<Long> questionBankIdList = batchRequest.getQuestionBankIdList();

        // 题目Id不能为空
        ThrowUtils.throwIf(questionIdList == null || questionIdList.isEmpty(),SysErrorCode.PARAMS_ERROR,"题目Id列表不能为空");

        // 批量添加
        questionIdList.stream().forEach(li->{
            QuestionBankQuestionRequest addRequest = new QuestionBankQuestionRequest();
            addRequest.setQuestionId(li);
            addRequest.setQuestionBankIdList(questionBankIdList);
            this.addQuestionBankQuestion(addRequest);
        });

        return true;
    }

    @Override
    public List<QuestionBankQuestionVo> getQuestionBankByQuestionId(Long questionId) {
        // 获取关联信息
        List<QuestionBankQuestion> questionBankQuestionList = this.list(Wrappers.<QuestionBankQuestion>lambdaQuery().eq(QuestionBankQuestion::getQuestionId, questionId));
        if (CollUtil.isEmpty(questionBankQuestionList) ){
            return null;
        }
        // 组装vo
        List<QuestionBankQuestionVo> questionBankQuestionVoList = questionBankQuestionList.stream().map(li->{
            QuestionBankQuestionVo questionBankQuestionVo = this.getQuestionBankQuestionVo(li);
            return questionBankQuestionVo;
        }).collect(Collectors.toList());

        return questionBankQuestionVoList;
    }

    @Override
    public Boolean removeQuestionByBankId(Long bankId) {
        boolean flag = this.remove(Wrappers.<QuestionBankQuestion>lambdaQuery().eq(QuestionBankQuestion::getQuestionBankId, bankId));
        ThrowUtils.throwIf(!flag, SysErrorCode.OPERATION_ERROR,"题库Id："+bankId+"，删除题目题库关联信息失败");
        return true;
    }


    public QuestionBankQuestionVo getQuestionBankQuestionVo(QuestionBankQuestion questionBankQuestion) {
        // 获取题库信息
        Long questionBankId = questionBankQuestion.getQuestionBankId();
        QuestionBank questionBank = questionBankService.getById(questionBankId);

        QuestionBankQuestionVo questionBankQuestionVo = new QuestionBankQuestionVo();
        questionBankQuestionVo.setId(questionBankQuestion.getId());
        questionBankQuestionVo.setQuestionBankId(questionBankId);
        questionBankQuestionVo.setQuestionId(questionBankQuestion.getQuestionId());
        questionBankQuestionVo.setQuestionBankName(questionBank.getTitle());

        return questionBankQuestionVo;
    }
}

